import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

/**
 * @author wuwenfa
 */
public class Main {

    public static void main(String[] args) throws Exception {
        String path = "D:\\study\\io";
        one(path, true);
        two(path);
        three("D:\\\\study\\\\io\\\\21\\\\12.txt");
        copyFile("D:\\study\\io\\21\\微信群收款.pdf", "D:\\study\\io\\211");
    }

    public static void one(String path, boolean flag) {
        //编写一段程序，判断d:/study/io目录是否存在，存在则遍历输出这个目录的所有文件名称，不存在则创建这个目录
        File filePath = new File(path);
        if (!filePath.exists()) {
            if (flag) {
                boolean res = filePath.mkdirs();
                if (res) {
                    System.out.println("目录不存在，已创建");
                } else {
                    System.out.println("目录不存在，创建失败");
                }
            }
        } else {
            String[] files = filePath.list();
            if (files != null && files.length != 0) {
                for (String f : files) {
                    String fp = path + File.separator + f;
                    File file = new File(fp);
                    if (file.isFile()) {
                        System.out.println(file.getName());
                    } else {
                        one(fp, false);
                    }
                }
            }
        }
    }

    public static void two(String path) {
        //编写一段程序，删除d:/study/io这个目录下的大于20Kb的文件
        File filePath = new File(path);
        if (filePath.exists()) {
            String[] files = filePath.list();
            if (files != null && files.length != 0) {
                for (String f : files) {
                    String fp = path + File.separator + f;
                    File file = new File(fp);
                    if (file.isFile()) {
                        if (file.length() > 1024 * 20) {
                            System.out.println(file.getPath());
                            boolean res = file.delete();
                            if (res) {
                                System.out.println(file.getPath() + "删除成功");
                            } else {
                                System.out.println(file.getPath() + "删除失败");
                            }
                        }
                    } else {
                        two(fp);
                    }
                }
            }
        }
    }

    public static void three(String path) throws Exception {
        //编写一段程序，循环从键盘输入一行字符，把这行字符首尾颠倒后按行输出到一个文件中，遇到quit后退出
        Scanner scanner = new Scanner(System.in);
        String line = scanner.nextLine();
        scanner.close();

        File f = new File(path);
        if(!f.getParentFile().exists()){
            boolean res = f.getParentFile().mkdirs();
            if (!res) {
                throw new Exception("文件创建失败");
            }
        }

        FileWriter fw = null;
        try {
            fw = new FileWriter(path);
            char[] c = line.toCharArray();
            for (int i = c.length - 1; i >= 0; i--) {
                if (c[i] == 't') {
                    if (c[i - 1] == 'i' && c[i - 2] == 'u' && c[i - 3] == 'q') {
                        break;
                    }
                }
                fw.write(c[i]);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                assert fw != null;
                fw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        System.out.println(line + " 写入成功");
    }

    public static void copyFile(String from, String to) throws Exception {
//        编写一个文件复制函数，copyFile(String from, String to)，from为原文件名，to为复制后的新文件名，复制结束打印
//        所复制的文件大小和用时（毫秒），执行结果如：copy xxx use xxx ms, file size is xxx.
        File fileFrom = new File(from);
        if (!fileFrom.exists() || !fileFrom.isFile()) {
            throw new Exception("来源文件异常");
        }
        File fileTo = new File(to);
        if (!fileTo.exists()) {
            boolean res = fileTo.mkdirs();
            if (!res) {
                throw new Exception("文件创建失败");
            }
        }
        FileReader fr = null;
        FileWriter fw = null;
        String fileToPath = fileTo + File.separator + fileFrom.getName();
        System.out.println(fileToPath);
        try {
            fr = new FileReader(fileFrom);
            fw = new FileWriter(fileToPath);

            long start = System.currentTimeMillis();
            int i = 0;
            while ((i = fr.read()) != -1) {
                fw.write(i);
            }
            long end = System.currentTimeMillis();
            System.out.println("copy " + fileFrom + " use " + (end - start) + " ms, file size is " + fileFrom.length() / 1024 + "kb");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                assert fr != null;
                fr.close();
                assert fw != null;
                fw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
